<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	function __construct()
	{
	    parent::__construct();
	    header("Content-type: text/html; charset=utf-8");
            $this->load->model('admin_model');
            $this->load->model('admin_log_model');
            $this->load->model('admin_group_model');    
	}
	
	public function index()
	{
            if($this->session->userdata('username')){
                    redirect('index');
            }
            $this->template->assign('_redirect', $_GET['redirect']);
            $this->template->load('login');
	}
	
	public function check()
	{
            if($_POST){
                $username	= trim($_POST['username']);
                $password	= trim($_POST['password']);
                $verify		= strtolower(trim($_POST['verify']));
                $vvcode		= $this->session->userdata('auth_code');
                $mssage = array();
                if(!$username||!$password||!$verify){
                    $mssage['info'] = '登录信息填写错误！';
                    $mssage['status'] = 0;
                    $mssage['url'] = '';
                } elseif($verify!=$vvcode){
                    $mssage['info'] = '验证码输入错误！';
                    $mssage['status'] = 0;
                    $mssage['url'] = '';
                } else{
                    $info = $this->admin_model->Get($username);
                    $password = md5($username.md5($password));
                    if($username!=$info['username']){
                            $mssage['info'] = '用户名不存在！';
                            $mssage['status'] = 0;
                            $mssage['url'] = '';
                    }elseif($password!=$info['password']){
                            $mssage['info'] = '登录密码不正确！';
                            $mssage['status'] = 0;
                            $mssage['url'] = '';
                    }else{
                        if($info['state'] == 0){
                            $mssage['info'] = '禁止登陆';
                            $mssage['status'] = 0;
                            $mssage['url'] = '';
                            echo json_encode($mssage);exit;
                        }
                        
                        $group_info = $this->admin_group_model->Get(intval($info['group_id']));
                        if($group_info['status'] == 0){
                            $mssage['info'] = '禁止登陆';
                            $mssage['status'] = 0;
                            $mssage['url'] = '';
                            echo json_encode($mssage);exit;
                        }
                        
                        $this->session->set_userdata($info);

                        $data = array();
                        $data['login_num']		 = $info['login_num']+1;
                        $data['last_login_ip']	 = $this->session->userdata('ip_address');
                        $data['last_login_time'] = date('Y-m-d H:i:s');
                        $this->admin_model->update($data);

                        $logdata = array();
                        $logdata['user_id'] = $info['id'];
                        $logdata['username'] = $info['username'];
                        $logdata['event'] = 'login';
                        $logdata['content'] = '登录后台';
                        $this->admin_log_model->insert($logdata);

                        $mssage['info'] = '登录成功！';
                        $mssage['status'] = 1;
                        $mssage['url'] = !empty($_POST['redirect']) ? urldecode(base64_decode($_POST['redirect'])) : site_url('index');
                    }
                }
                echo json_encode($mssage);exit;
            }
	}
	
	public function quit()
	{
		$this->session->sess_destroy();
		redirect('login');
	}
}
